package com.sky.controller.admin;


import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.service.impl.DishServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.LinkedList;
import java.util.List;
import java.util.Set;

@RestController
@RequestMapping("/admin/dish")
@Slf4j
@Api(tags = "菜品相关接口")
public class DishController {

    @Autowired
    private DishService dishService;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 新增菜品
     * @param dishDTO
     * @return
     */
    @PostMapping
    @ApiOperation("新增菜品")
    public Result saveWithFlavors(@RequestBody DishDTO dishDTO){
        dishService.savaWithFlavors(dishDTO);

        //清理redis缓存数据
        String key = "dish_" + dishDTO.getCategoryId();
        cleanCache(key);

        return Result.success();
    }

    /**
     * 菜品分页查询
     */
    @GetMapping("/page")
    @ApiOperation("菜品分页查询")
    public Result<PageResult> dishPage(DishPageQueryDTO dto){
        PageResult pageResult = dishService.dishPage(dto);
        return Result.success(pageResult);
    }

    /**
     * 菜品批量删除
     */
    @DeleteMapping
    @ApiOperation("菜品批量删除")
    public Result dishDelete(@RequestParam List<Long> ids){//注解将请求中的指定名称的参数赋值给方法中的形参
        dishService.dishDelete(ids);

        //将所有的菜品缓冲数据清理掉，所有以dish_开头的
        cleanCache("dish_*");
        return Result.success();
    }

    /**
     * 菜品起售或停止
     */
    @PostMapping("/status/{status}")
    @ApiOperation("菜品起售或停止")
    public Result startOrStop(@PathVariable String status, String id){
        dishService.startOrStop(status,id);

        //将所有的菜品缓冲数据清理掉，所有以dish_开头的
        cleanCache("dish_*");
        return Result.success();
    }

    /**
     * 修改菜品信息
     */
    //根据ID回显数据
    @GetMapping("/{id}")
    public Result<DishDTO> getById(@PathVariable String id){
        DishDTO dishDTO = dishService.getById(id);
        return Result.success(dishDTO);
    }
    @PutMapping
    @ApiOperation("修改菜品信息")
    public Result dishUpdate(@RequestBody DishDTO dishDTO){
        dishService.dishUpdate(dishDTO);

        //将所有的菜品缓冲数据清理掉，所有以dish_开头的
cleanCache("dish_*");
        return Result.success();
    }

    /**
     * 根据分类id查询菜品
     * @param categoryId
     * @return
     */
    @GetMapping("/list")
    @ApiOperation("根据分类id查询菜品")
    public Result<List<Dish>> list(Long categoryId){
        Dish dish = new Dish();
        dish.setCategoryId(categoryId);
        dish.setStatus(StatusConstant.ENABLE);
        List<Dish> list = dishService.list(categoryId);
        return Result.success(list);
    }


    /**
     * 统一清理redis缓存的方法
     */
    private void cleanCache(String pattern){
        Set keys = redisTemplate.keys(pattern);
        redisTemplate.delete(keys);
    }

}
